package pers.whj.net.local.protocol;

public class Protocol {
    public static final int PORT_UDP_SERVER = 8888;

    public static final int RECONNECT_UDP = 4;

    public static final int MAX_PACKETS_UDP = 100;

    public static final int TIMEOUT_UDP_CLIENT = 500;
    public static final int TIMEOUT_TCP_CONNECT = 5000;
    public static final int TIMEOUT_TCP_READ = 3000;
    public static final int TIMEOUT_CHECK_STATE = 1000;
    public static final int TIME_TO_END_CONNECTION = 30 * 1000;

    public static final int OFFSET = 1;
    public static final int LENGTH_READ = 1024 * 4;

    //command: 1 ~ 127
    public static final byte INVALID = 48;
    public static final byte CHECK_STATE = 49;
    public static final byte END_CONNECTION = 50;
    public static final byte REQ_PORT = 51;
    public static final byte RES_PORT = 52;
    public static final byte REQ_UPLOAD = 53;
    public static final byte REQ_DOWNLOAD = 54;
    public static final byte REQ_REMOTE_FILE_INFO = 55;
    public static final byte REQ_DELETE_REMOTE_FILE = 56;
    public static final byte REQ_RENAME_REMOTE_FILE = 57;
    public static final byte REQ_CREATE_REMOTE_FILE = 58;
    public static final byte DATA = 59;
    public static final byte DATA_END = 60;
    public static final byte RESPONSE = 61;
    public static final byte SUCCESS = 62;
    public static final byte FAIL = 63;

    public static boolean validCommand(byte command) {
        return command >= 1;
    }

    public static void fillCommand(byte command, byte[] data) {
        data[0] = command;
    }

    public static byte[] getData(byte command, String info) {
        if (!validCommand(command)) {
            command = INVALID;
        }
        if (info == null) {
            info = "";
        }
        info = (char) command + info;
        return info.getBytes();
    }

    public static byte getCommand(byte[] data) {
        return data[0];
    }

    public static String getInfo(byte[] data, int dataLength) {
        return new String(data, OFFSET, dataLength - OFFSET);
    }
}